﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Entity;

namespace Project_V1
{
    public partial class FrmReports : Form
    {
        ReportService objReportService = new ReportService();

        //private Report objReport;//DGV初始加载和选择非当前行触发RowEnter事件,创建objReport对象保存信息,做报表展示

        private int reportListId=-1;
        private List<ProcessData> ProcessDataList = null;
        public static FrmGraphs objFrm = null;
        public int ii=0;
        public FrmReports()
        {
            InitializeComponent();
            this.Load += FrmReports_Load;
        }

        private int startId;
        private int endId;
        private DateTime t1;
        private DateTime t2;
        private DateTime t3;
        private void FrmReports_Load(object sender, EventArgs e)
        {
            CommonMethods.ReportsList = objReportService.GetTop30();//默认读取最近30次报表集合
            this.dgv_data.Columns["UpdateTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm;ss";
            this.dgv_data.AutoGenerateColumns = false;//只显示自定义的列
            this.dgv_data.DataSource = CommonMethods.ReportsList;
            DateTime dt = DateTime.Now.Date;
            this.dt_StartPoint.Value = dt;

            this.dateTimePicker2.Text = this.dateTimePicker2.Value.AddDays(-300).ToString();
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
             t1 = Convert.ToDateTime(this.dt_StartPoint.Text + " 00:00:00");
             t2 = Convert.ToDateTime(this.dt_EndPoint.Text + " 23:59:59");
            startId = int.Parse(this.txt_StartNumber.Text.Trim());
            endId = int.Parse(this.txt_EndNumber.Text.Trim());

            if (t1 > t2)
            {
                MessageBox.Show("开始时间必须小于结束时间", "查询提示");
                return;
            }
            if (startId > endId)
            {
                MessageBox.Show("起始规范号必须小于等于结束规范号", "查询提示");
                return;
            }

            CommonMethods.ReportsList = objReportService.GetTop30(startId, endId,t1,t2);//查询后存入报表集合
            UpdateDGV();            

        }

        private void btn_Preview_Click(object sender, EventArgs e)
        {
            ProcessDataList = objReportService.GetProcessDataById(reportListId);//执行数据库查询,得到曲线数据集合
            if (new Toexcel().ListShowToExcel(ProcessDataList, true)==false)
            {
                MessageBox.Show("预览失败,请检查是否有数据或Office是否安装", "预览提示");
            }
        }
        //导出按钮
        private void btn_Export_Click(object sender, EventArgs e)
        {

            string savetime = "";
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = @"C:\dt"; // 设置默认保存路径
            saveFileDialog.Title = "导出曲线数据";
            saveFileDialog.Filter = "Text files (*.csv)|*.csv|All files (*.*)|*.*";
            savetime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
       
            for(int i = 0; i< dgv_data.Rows.Count; i++)
            {
                if (dgv_data.SelectedRows.Count > 0)
                { 
                    DataGridViewRow selectedRow = dgv_data.SelectedRows[0]; 
                    savetime = Convert.ToDateTime( selectedRow.Cells["Updatetime"].Value.ToString()).ToString("yyyyMMddHHmmss"); 
                } 
            }
            saveFileDialog.FileName = "曲线数据" + savetime;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            { 
                ProcessDataList = objReportService.GetProcessDataById(reportListId);//执行数据库查询,得到曲线数据集合  
                Toexcel.ListToCSV(ProcessDataList, saveFileDialog.FileName); 
            }

            MessageBox.Show("曲线数据保存成功");
 


            //ProcessDataList = objReportService.GetProcessDataById(reportListId);//执行数据库查询,得到曲线数据集合
            //new Toexcel().ListToExcel(ProcessDataList);

        }

        //点击查询曲线,查询数据库存入集合,打开曲线界面
        private void btn_Chart_Click(object sender, EventArgs e)
        {
            objFrm = null;
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要查询的曲线数据！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要查询的行！", "提示信息");
                return;
            }

            ProcessDataList =objReportService.GetProcessDataById(reportListId);//执行数据库查询,得到曲线数据集合

            Beckhoff_PLC.AX.Clear();
            Beckhoff_PLC.AX1.Clear();
            Beckhoff_PLC.AX2.Clear();
            Beckhoff_PLC.AY1.Clear();
            Beckhoff_PLC.AY2.Clear();
            Beckhoff_PLC.AY3.Clear();
            Beckhoff_PLC.AY4.Clear();
            ii = 0;
            //for (int i = 0; i < 500; i++)
            //{
            //    Beckhoff_PLC.AX1.Add(10 * i);
            //}
            foreach (ProcessData item in ProcessDataList)//将集合对象拆分到5个集合
            {                
                Beckhoff_PLC.AY1.Add(item.DataPressure);
                Beckhoff_PLC.AY2.Add(item.DataTorque);
                Beckhoff_PLC.AY3.Add(item.ActVelo1);
                Beckhoff_PLC.AY4.Add(item.ActVelo2);
                Beckhoff_PLC.AX.Add(item.ActPos1);
                Beckhoff_PLC.AX2.Add(item.ActPos1);
                Beckhoff_PLC.AX1.Add(10 * ii);
                ii = ii + 1;
            }
            if (objFrm == null)
            {
                objFrm = new FrmGraphs();
                objFrm.FormBorderStyle = FormBorderStyle.FixedSingle;
                objFrm.ShowDialog();
            }
            else
            {
                objFrm.Activate();//激活只能在最小化的时候起作用
                objFrm.WindowState = FormWindowState.Normal;
            }
        }

        #region 查询曲线(集合)数据
        
        #endregion

        #region 被选中行RowEnter事件,得到被选中行的索引值,和SettingsList集合的索引一一对应
        private void dgv_data_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //DGV初始加载和选择非当前行触发RowEnter事件,该事件结束后CurrentRow属性才会被更新
            CommonMethods.objReport = new Report();
            int i = e.RowIndex;//索引值按照由小到大,集合由数据库默认按照主键增序存入,索引一一对应
            CommonMethods.objReport = CommonMethods.ReportsList[i];
            reportListId = CommonMethods.objReport.ReportListId;
        }
        #endregion
        
        #region UpdateDGV()从数据库读取信息存入集合,重新绑定刷新DataGridView
        private void UpdateDGV()//从数据库读取集合信息
        {
            this.dgv_data.Columns["UpdateTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm;ss";
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = CommonMethods.ReportsList;
        }

        #endregion

        //双击dgv一栏实现对该栏的修改操作
        private void dgv_data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.btn_Chart_Click(null, null);
        }

        #region 工艺规范号文本框TextChanged时间做输入验证        
        private void txt_StartNumber_TextChanged(object sender, EventArgs e)
        {
            if (!DataValidate.IsInteger(this.txt_StartNumber.Text.Trim()))
            {
                MessageBox.Show("必须填入整数", "填写错误");
                return;
            }
            if (int.Parse(this.txt_StartNumber.Text.Trim()) < 1)
            {
                MessageBox.Show("填入数据不得小于" + 1, "填写错误");
                return;
            }
            if (int.Parse(this.txt_StartNumber.Text.Trim()) > 99)
            {
                MessageBox.Show("填入数据不得大于" + 99, "填写错误");
                return;
            }
            startId = int.Parse(this.txt_StartNumber.Text.Trim());
        }

        private void txt_EndNumber_TextChanged(object sender, EventArgs e)
        {
            if (!DataValidate.IsInteger(this.txt_EndNumber.Text.Trim()))
            {
                MessageBox.Show("必须填入整数", "填写错误");
                return;
            }
            if (int.Parse(this.txt_EndNumber.Text.Trim()) < 1)
            {
                MessageBox.Show("填入数据不得小于" + 1, "填写错误");
                return;
            }
            if (int.Parse(this.txt_EndNumber.Text.Trim()) > 99)
            {
                MessageBox.Show("填入数据不得大于" + 99, "填写错误");
                return;
            }
            endId = int.Parse(this.txt_EndNumber.Text.Trim());
        }
        #endregion

        private void tb_PadNum_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmKeyPadNum.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmKeyPadNum frmSoftKey = new FrmKeyPadNum(tb_temp.Text);
            // 计算窗体的位置
            //textboxLocation.X = 1;
            //textboxLocation.Y = 1;
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmKeyPadNum.confirm)
            {
                tb_temp.Text = FrmKeyPadNum.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }

        private void BottomPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            if (MessageBox.Show("是否删除历史记录", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
            }
            else
            {
                try
                {
                    t3 = Convert.ToDateTime(this.dateTimePicker2.Text);
                    startId = int.Parse(this.txt_StartNumber.Text.Trim());
                    endId = int.Parse(this.txt_EndNumber.Text.Trim());
                    objReportService.Delete(t3);//查询后存入报表集合
                }
                catch
                { 
                } 
            }
        }
    }
}
